<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Api\Controllers\Auths;

use App\Api\BaseApi;
use App\Models\BeanTransaction;
use App\Models\DailyBeanReleases;
use App\Models\GoldBeanAccount;
use App\Models\OrderBeanRewards;
use App\Models\PointAccount;
use App\Models\PointTransaction;
use App\Models\RewardTransaction;
use App\Models\SinglePool;
use App\Models\SinglePoolTransaction;
use App\Models\UserBeanPackages;
use App\Models\UserRewardAccount;
use App\Models\VoucherPackage;
use App\Models\VoucherPackageTransaction;
use App\Models\WalletAccount;
use App\Models\WalletTransaction;

class AccountApi extends BaseApi
{
    public function getBeanAccount()
    {
        $account = GoldBeanAccount::query()
            ->where('user_id', self::UserId())
            ->first();
        return responses($account);
    }

    public function getBeanAccountTransaction()
    {
        $account = BeanTransaction::query()
            ->where('user_id', self::UserId())
            ->latest('id')
            ->simplePaginate(request('perPage', 20));
        return responses($account);
    }

    public function getWalletAccount()
    {
        $account = WalletAccount::query()
            ->where('user_id', self::UserId())
            ->first();
        return responses($account);
    }

    public function getWalletAccountTransaction()
    {
        $account = WalletTransaction::query()
            ->where('user_id', self::UserId())
            ->latest('id')
            ->simplePaginate(request('perPage', 20));
        return responses($account);
    }

    public function getPointAccount()
    {
        $account = PointAccount::query()
            ->where('user_id', self::UserId())
            ->first();
        return responses($account);
    }

    public function getPointAccountTransaction()
    {
        $account = PointTransaction::query()
            ->where('user_id', self::UserId())
            ->latest('id')
            ->simplePaginate(request('perPage', 20));
        return responses($account);
    }

    public function getRewardAccount()
    {
        $account = UserRewardAccount::query()
            ->where('user_id', self::UserId())
            ->first();
        return responses($account);
    }

    public function getRewardAccountTransaction()
    {
        $account = RewardTransaction::query()
            ->where('user_id', self::UserId())
            ->latest('id')
            ->simplePaginate(request('perPage', 20));
        return responses($account);
    }

    public function beanReleaseDaily()
    {
        $res = OrderBeanRewards::query()
            ->where('user_id', self::UserId())
            ->where('remaining_beans', '>', 0)
            ->latest('id')
            ->simplePaginate(request('perPage', 20));
        return responses($res);
    }

    public function beanPackageReleaseDaily()
    {
        $res = DailyBeanReleases::with('source')
            ->where('user_id', self::UserId())
            ->where('source_type', UserBeanPackages::class)
            ->simplePaginate(request('perPage', 20));
        return responses($res);
    }

    public function exchangeCoupon()
    {
        $res = VoucherPackage::query()
            ->where('user_id', self::UserId())
            ->where('type', 1)
            ->first();
        return responses($res);
    }

    public function exchangeCouponInfo()
    {
        $res = VoucherPackageTransaction::query()
            ->where('user_id', self::UserId())
            ->where('type', 1)
            ->latest('id')
            ->paginate(request('perPage', 20));
        return responses($res);
    }

    public function getSinglePool()
    {
        $res = SinglePool::query()
            ->where('user_id', self::UserId())
            ->first();
        return responses($res);
    }

    public function getSinglePoolTransaction()
    {
        $res = SinglePoolTransaction::query()
            ->where('user_id', self::UserId())
            ->when(self::$request->get('type'), function ($query, $value) {
                return $query->where('change_type', $value);
            })
            ->latest('id')
            ->paginate(request('perPage', 20));
        return responses($res);
    }
}
